<?php
declare (strict_types=1);

namespace app\school\model\school\admin;

use app\admin\logic\user\Account as AdminAccount;
use app\common\logic\Attach;
use app\common\model\BaseModel;
use app\school\event\subscribes\school\SchoolLog as SchoolLogSubscribes;
use app\user\logic\user\Account;
use think\facade\Event;
use think\helper\Str;
use think\model\concern\SoftDelete;

/**
 * 定义机构数据模型
 */
class School extends BaseModel
{
    use SoftDelete;

    /**
     * 表名
     *
     * @var string
     */
    protected $name = 'school';

    /**
     * 设置自动写入时间
     *
     * @var boolean
     */
    protected $autoWriteTimestamp = true;
    /**
     * 时间显示格式
     *
     * @var string
     */
    protected $dateFormat = 'Y-m-d H:i';

    /**
     * 填充来源
     *
     * @var [type]
     */
    protected $append = ['from_text', 'status_text'];

    /**
     * 定义字段类型
     *
     * @var array
     */
    protected $type = [
        'id'               => 'integer',
        'status'           => 'integer',
        'fullcategorypath' => 'json',
    ];

    /**
     * 定义表主键
     *
     * @var string
     */
    protected $pk = 'id';

    /**
     * 新增数据事件
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-03-03
     */
    public static function onBeforeInsert($school)
    {
        $school->school_type = 1;
        $school->from        = 'admin';
        $school->school_code = School::getSchoolCode();
    }

    /**
     * 产生机构编码
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/6/28
     *
     * @static
     * @return string
     */
    public static function getSchoolCode()
    {
        return 'JG' . Str::random(15, 2, '1234567890');
    }

    /**
     * 机构来源
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-27
     *
     * @param string $value [description]
     * @param    [type]                         $data  [description]
     * @return   [type]                                [description]
     */
    public function getFromTextAttr($value = '', $data): string
    {
        $text = [
            'admin' => '管理员创建',
        ];

        return $text[$data['from']] ?? $data['from'] . '端申请入驻';
    }

    /**
     * 机构状态
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-06-04
     *
     * @param string $value [description]
     * @param    [type]                         $data  [description]
     * @return   [type]                                [description]
     */
    public function getStatusTextAttr($value = '', $data): string
    {
        $text = [
            0 => '机构被禁用',
            1 => '正常',
            2 => '待审核',
            3 => '审核未通过',
        ];

        return $text[$data['status']] ?? '未知';
    }

    /**
     * 设置状态查询条件
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-03-10
     *
     * @param    [type]                         $query  [description]
     * @param integer $status 查询条件值
     */
    public function scopeStatus($query, $status = 1)
    {
        $query->where('status', $status);
    }

    /**
     * 机构等级
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-28
     *
     * @return   [type]                         [description]
     */
    public function level()
    {
        return $this->belongsTo(SchoolLevel::class, 'level_id')->bind(['level_text' => 'title']);
    }

    /**
     * logo地址
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-28
     *
     * @return   [type]                         [description]
     */
    public function logo()
    {
        return $this->belongsTo(Attach::class, 'logo')->bind(['logo_url' => 'fileurl']);
    }

    /**
     * 身份证-正面
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-28
     *
     * @return   [type]                         [description]
     */
    public function iDcardPositive()
    {
        return $this->belongsTo(Attach::class, 'legal_IDcard_positive')->bind(['legal_IDcard_positive_url' => 'fileurl']);
    }

    /**
     * 身份证-反面
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-28
     *
     * @return   [type]                         [description]
     */
    public function iDcardSide()
    {
        return $this->belongsTo(Attach::class, 'legal_IDcard_side')->bind(['legal_IDcard_side_url' => 'fileurl']);
    }

    /**
     * 执照
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-28
     *
     * @return   [type]                         [description]
     */
    public function businessLicense()
    {
        return $this->belongsTo(Attach::class, 'business_license')->bind(['business_license_url' => 'fileurl']);
    }

    /**
     * 当前机构的管理员
     *
     * @return \think\model\relation\BelongsTo
     */
    public function admin()
    {
        return $this->belongsTo(AdminAccount::class, 'admin_user_id');
    }

    /**
     * 当前机构申请人信息
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-29
     *
     * @return   [type]                         [description]
     */
    public function apply()
    {
        return $this->belongsTo(Account::class, 'user_id');
    }

    /**
     * 当前机构申请人信息
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-29
     *
     * @return   [type]                         [description]
     */
    public function applyPhone()
    {
        return $this->belongsTo(Account::class, 'user_id')->bind(['phone']);
    }

    /**
     * 成功创建之后
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-06-17
     */
    public static function onAfterInsert($school)
    {
        if (!Event::hasListener('SchoolCreated')) {
            Event::subscribe(SchoolLogSubscribes::class);
        }
        Event::trigger('SchoolCreated', $school);
    }

    /**
     * 更新事件
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2022-03-09
     *
     * @param  [type] $school [description]
     * @return [type]         [description]
     */
    public static function onBeforeUpdate($school)
    {
        $s = app(School::class)->where('id', $school->id)->value('status');
        cache('school_' . $school->id . '_status_before_update', $s, 1800);
    }

    /**
     * 成功修改之后
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-06-17
     */
    public static function onAfterUpdate($school)
    {
        if (!Event::hasListener('SchoolUpdated')) {
            Event::subscribe(SchoolLogSubscribes::class);
        }
        Event::trigger('SchoolUpdated', $school);
    }

    /**
     * 成功删除之后
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-06-17
     */
    public static function onAfterDelete($school)
    {
        if (!Event::hasListener('SchoolDeleted')) {
            Event::subscribe(SchoolLogSubscribes::class);
        }
        Event::trigger('SchoolDeleted', $school);
    }

}
